﻿#region Copyright (c) 2000-2008 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{       AgDataGrid                                 }
{                                                                   }
{       Copyright (c) 2000-2008 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2008 Developer Express Inc.

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using DevExpress.AgMenu.Utils;
namespace DevExpress.AgMenu.DefaultStyle {
	public class AgSubMenuDefaultStyleHelper : System.Windows.Controls.Control {
		protected AgMenuItem Item { get; set; }
		public AgSubMenuDefaultStyleHelper() {
			DefaultStyleKey = typeof(AgSubMenuDefaultStyleHelper);
			LayoutUpdated += OnLayoutUpdated;
		}
		protected virtual void AdjustBounds() {
#if !DEBUGTEST
			Point p = Item.GetPositionNew(null);
			Rect itemBounds = new Rect(p.X, p.Y, Item.ActualWidth, Item.ActualHeight);
			p = ((FrameworkElement)VisualTreeHelper.GetParent(this)).GetPositionNew(null);
			AgPopupMenu popupMenu = Item.SubMenu;
			if (popupMenu.RootMenu.GetOrientation() == Orientation.Horizontal && p.Y > itemBounds.Top) {
				Width = itemBounds.Width;
				if (Item.Position != AgMenuItemPosition.Last && Item.Position != AgMenuItemPosition.Single)
					Width--;
				Height = 3;
				Margin = new Thickness(itemBounds.X - p.X, 0, 1, 0);
				if (p.Y > itemBounds.Top)
					VerticalAlignment = VerticalAlignment.Top;
				else
					VerticalAlignment = VerticalAlignment.Bottom;
			} else {
			}
#endif
		}
		protected virtual void OnLayoutUpdated(object sender, EventArgs e) {
			if (Item != null) return;
			AgPopupMenu popupMenu = FindPopupMenu();
			if (!(popupMenu.PopupElement is AgMenuItemBase)) return;
			AgMenuItem item = (AgMenuItem)popupMenu.PopupElement;
			if (item.Menu == null || item.Menu.IsPopupMenu) return;
			Item = item;
			Item.PopupAnimate += OnItemPopupAnimate;
			Item.SubMenuClosed += OnItemSubMenuClosed;
		}
		protected virtual void OnItemPopupAnimate(object sender, AgMenuItemPopupAnimateEventArgs e) {
			if (e.IsShowing)
				AdjustBounds();
		}
		protected virtual void OnItemSubMenuClosed(object sender, EventArgs e) {
			Item.PopupAnimate -= OnItemPopupAnimate;
			Item.SubMenuClosed -= OnItemSubMenuClosed;
		}
		AgPopupMenu FindPopupMenu() {
			return VisualTreeUtils.FindRoot<AgPopupMenu>(this);
		}
	}
	public class AgMenuSubItemArrowDefaultStyleHelper : System.Windows.Controls.Control {
		public AgMenuSubItemArrowDefaultStyleHelper() {
			DefaultStyleKey = typeof(AgMenuSubItemArrowDefaultStyleHelper);
			Loaded += new RoutedEventHandler(OnLoaded);
		}
		protected virtual void OnLoaded(object sender, RoutedEventArgs e) {
			AgMenuItemBase item = FindItem();
			this.SetVisible(item != null && item.IsSubItem);
		}
		AgMenuItemBase FindItem() {
			return VisualTreeUtils.FindRoot<AgMenuItemBase>(this);
		}
	}
	public class AgMenuDefaultStyleSubMenuStyleSelector : AgMenuSubMenuStyleSelector {
		public override Style SelectStyle(AgPopupMenu subMenu) {
			Style result = new Style() { TargetType = typeof(AgPopupMenu) };
			AgMenuBase ownerMenu = ((AgMenuItemBase)subMenu.PopupElement).Menu;
			Thickness popupOffset;
			if (ownerMenu.IsRootMenu) {
				if (ownerMenu.GetOrientation() == Orientation.Horizontal)
					popupOffset = new Thickness(1, -3, 0, -3);
				else
					popupOffset = new Thickness(-3, 1, 1, 0);
			} else
				popupOffset = new Thickness(-3, 1, 1, 0);
			result.Setters.Add(new Setter(AgPopupMenu.PopupOffsetProperty, popupOffset));
			return result;
		}
	}
	public abstract class AgMenuItemDefaultStyleHelper : System.Windows.Controls.Control {
		public static readonly DependencyProperty PositionProperty = DependencyProperty.Register("Position", typeof(AgMenuItemPosition), typeof(AgMenuItemDefaultStyleHelper),
			new PropertyMetadata(delegate(DependencyObject d, DependencyPropertyChangedEventArgs e) {
			((AgMenuItemDefaultStyleHelper)d).PositionChanged();
		}));
		protected const double CornerRadius = 4;
		public AgMenuItemPosition Position {
			get { return (AgMenuItemPosition)GetValue(PositionProperty); }
			set { SetValue(PositionProperty, value); }
		}
		public AgMenuItemDefaultStyleHelper() {
			Loaded += OnLoaded;
		}
		protected abstract void UpdateTemplateProperties();
		void OnLoaded(object sender, RoutedEventArgs e) {
			UpdateTemplateProperties();
		}
		void PositionChanged() {
			if (this.GetParent() != null)
				UpdateTemplateProperties();
		}
	}
	public class AgHorizontalMenuItemDefaultStyleHelper : AgMenuItemDefaultStyleHelper {
		Border LeftBorder {
			get { return (Border)((Grid)this.GetParent()).Children[1]; }
		}
		Border RightBorder {
			get { return (Border)((Grid)this.GetParent()).Children[3]; }
		}
		Border SurroundBorder {
			get { return (Border)((Grid)this.GetParent()).GetParent(); }
		}
		protected override void UpdateTemplateProperties() {
			switch (Position) {
				case AgMenuItemPosition.First:
					SurroundBorder.CornerRadius = new CornerRadius(CornerRadius, 0, 0, CornerRadius);
					break;
				case AgMenuItemPosition.Middle:
					SurroundBorder.CornerRadius = new CornerRadius(0, 0, 0, 0);
					break;
				case AgMenuItemPosition.Last:
					SurroundBorder.CornerRadius = new CornerRadius(0, CornerRadius, CornerRadius, 0);
					break;
				case AgMenuItemPosition.Single:
					SurroundBorder.CornerRadius = new CornerRadius(CornerRadius, CornerRadius, CornerRadius, CornerRadius);
					break;
				default:
					throw new Exception();
			}
			LeftBorder.Opacity = (Position == AgMenuItemPosition.Middle || Position == AgMenuItemPosition.Last) ? 1 : 0;
			RightBorder.Opacity = (Position == AgMenuItemPosition.First || Position == AgMenuItemPosition.Middle) ? 1 : 0;
		}
	}
	public class AgVerticalMenuItemDefaultStyleHelper : AgMenuItemDefaultStyleHelper {
		Border BottomBorder {
			get { return (Border)((Grid)this.GetParent()).Children[3]; }
		}
		Border SurroundBorder {
			get { return (Border)((Grid)this.GetParent()).GetParent(); }
		}
		Border TopBorder {
			get { return (Border)((Grid)this.GetParent()).Children[1]; }
		}
		protected override void UpdateTemplateProperties() {
			switch (Position) {
				case AgMenuItemPosition.First:
					SurroundBorder.CornerRadius = new CornerRadius(CornerRadius, CornerRadius, 0, 0);
					break;
				case AgMenuItemPosition.Middle:
					SurroundBorder.CornerRadius = new CornerRadius(0, 0, 0, 0);
					break;
				case AgMenuItemPosition.Last:
					SurroundBorder.CornerRadius = new CornerRadius(0, 0, CornerRadius, CornerRadius);
					break;
				case AgMenuItemPosition.Single:
					SurroundBorder.CornerRadius = new CornerRadius(CornerRadius, CornerRadius, CornerRadius, CornerRadius);
					break;
				default:
					throw new Exception();
			}
			TopBorder.Opacity = (Position == AgMenuItemPosition.Middle || Position == AgMenuItemPosition.Last) ? 1 : 0;
			BottomBorder.Opacity = (Position == AgMenuItemPosition.First || Position == AgMenuItemPosition.Middle) ? 1 : 0;
		}
	}
}
